package com.pi.base.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pi.base.common.BaseResult;
import com.pi.base.constant.SysStatics;
import com.pi.base.entity.SysMenu;
import com.pi.base.entity.SysMenuExample;
import com.pi.base.mapper.BaseSysRoleMapper;
import com.pi.base.mapper.SysMenuMapper;
import com.pi.base.mapper.SysRoleMapper;
import com.pi.base.service.BaseService;
import com.pi.base.service.ISysMenuService;
import com.pi.base.util.MapBuilder;
import com.pi.base.util.Prop;
import com.pi.base.util.PropKit;
import com.pi.base.util.ValidUtil;

@Service("sysMenuService")
public class SysMenuService extends BaseService<SysMenu> implements ISysMenuService{
	
	private final Logger logger = LoggerFactory.getLogger(SysMenuService.class);
			
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private BaseSysRoleMapper baseSysRoleMapper ;
	
	@Override
	public SysMenu sysMenuInfoById(int sysMenuId) throws Exception {
		return sysMenuMapper.selectByPrimaryKey(sysMenuId);
	}

	@Override
	public BaseResult delParentMenu(int parentMenuId) throws Exception {
		PropKit.use("tableMap.properties");
		String table_sys_menu = PropKit.get("sys_menu", "sys_menu");
		Map<String, String> map = new MapBuilder.Builder<String>().
				map("menuId",parentMenuId+"").
				map("tableName",table_sys_menu).build().map();
		List<SysMenu> menuList =  baseSysRoleMapper.queryMenuWithSubByID(map);
		if(ValidUtil.isEmptyList(menuList)){
			return BaseResult.newErrorResult("没有菜单可以删除");
		}
		Iterator<SysMenu> it = menuList.iterator();
		while(it.hasNext()){
			sysMenuMapper.deleteByPrimaryKey(it.next().getId());
		}
		return BaseResult.newSuccessResult("删除成功");
	}

	@Override
	public void insertSysMenu(SysMenu sysMenu) throws Exception {
		sysMenuMapper.insertSelective(sysMenu);
	}

	@Override
	public int countSysMenuByCode(String urlCode) throws Exception {
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andUrlEqualTo(urlCode);
		return sysMenuMapper.selectCountByExample(example);
	}

	@Override
	public List<SysMenu> getMenuListByParentId(int id) throws Exception {
		PropKit.use("tableMap.properties");
		Map<String, String> map = new MapBuilder.Builder<String>().
				map("tableName",PropKit.get("sys_menu","sys_menu")).
				map("function1",PropKit.get("queryMenuWithSubByID","queryMenuWithSubByID")).
				map("menuId",id+"").build().map();
		return baseSysRoleMapper.queryMenuWithSubByID(map);
	}
	
	@Override
	public List<SysMenu> getAllMenuListLevel2() throws Exception {
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andLevelEqualTo(SysStatics.SysMenu_Level_Two);
		return sysMenuMapper.selectByExample(example);
	}
	
	@Override
	public List<SysMenu> getMenuListByExample(SysMenuExample example)
			throws Exception {
		return sysMenuMapper.selectByExample(example);
	}

	
}
